package com.gleason.gt.server.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gleason.gt.data.service.MusicFileService;
import com.gleason.gt.data.service.PlaylistEntryService;
import com.gleason.gt.server.comparison.PlayListEntryComparator;
import com.gleason.gt.server.model.Playlist;
import com.gleason.gt.server.model.Rating;
import com.gleason.gt.server.model.database.PlayListEntry;
import com.thoughtworks.xstream.XStream;

@Controller
@RequestMapping(value = "/playlist")
public class PlaylistController {
	@Autowired
	private PlaylistEntryService playlistService;
	@Autowired
	private MusicFileService musicService;
	
	List<PlayListEntry> playlistItems = new ArrayList<PlayListEntry>();
	
	private PlayListEntry getPlaylistEntryById(Integer id){
		for (PlayListEntry entry : playlistItems) {
			if(entry.getId().equals(id)){
				return entry;
			}	
		}
		return null;
	}
	
	@RequestMapping(value="/addThumbsUp", method = RequestMethod.POST)
	protected ModelAndView thumbsUp(@RequestParam(value="playlistEntryId") Integer pleId, HttpServletRequest request) {
		Map<String, Object> options = new HashMap<String, Object>();
		getPlaylistEntryById(pleId).getCurrentRating().add(Rating.THUMBS_UP);
		return new ModelAndView("options", options);
	}
	@RequestMapping(value="/addThumbsDown", method = RequestMethod.GET)
	protected ModelAndView thumbsDown(@RequestParam(value="requestId") Integer requestId, HttpServletRequest request) {
		Map<String, Object> options = new HashMap<String, Object>();
		if(playlistItems.size()>=requestId){
			playlistItems.get(--requestId).getCurrentRating().add(Rating.THUMBS_UP);
		}
		if(playlistItems.get(requestId).getTotalRating()<0){
			playlistItems.remove(requestId);
		}
		return new ModelAndView("options", options);
	}
	private void refreshPlaylist(){
		playlistItems = playlistService.getPlaylist();
		for (PlayListEntry pe : playlistItems) {
			pe.setFile(musicService.getMusicFile(pe.getFileId()));
		}
	}
	@RequestMapping(value="/getPlaylist", method = RequestMethod.GET)
	public @ResponseBody String getPlaylist() throws SQLException{
		refreshPlaylist();
		XStream xstream = new XStream();
		try{
			Collections.sort(playlistItems, new PlayListEntryComparator());
			return xstream.toXML(playlistItems);
		}
		catch(Exception ex){
			//TODO:add handleing
		}
		return null;
	}
	@RequestMapping(value="/nextSong", method = RequestMethod.GET)	
	public @ResponseBody byte[] getNextSong() throws SQLException{
		Collections.sort(playlistItems, new PlayListEntryComparator());
		return playlistItems.get(0).getFile().getFile();
	}
}
